import React from 'react';
import {Modal, Button} from 'antd';
import {WeaTools, WeaLocaleProvider, WeaDialog, WeaRightMenu, WeaMoreButton, WeaTab} from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import {WeaTableNew} from 'comsMobx';
const {WeaTable, TableStore} = WeaTableNew;
const tableStore = new TableStore();

import './style/';
import Confirm from './Confirm'

class WeaNonStandard extends React.Component {
    state = {visible: false, loading: false, templateType: '', templateName: '', selectedRowKeys: [], data: {}};

    constructor(props) {
        super(props);
        this.getButtons = this.getButtons.bind(this);
        this.getRightMenus = this.getRightMenus.bind(this);
        this.getRowSelection = this.getRowSelection.bind(this);
        this.getData = this.getData.bind(this);
        this.onUse = this.onUse.bind(this);
        this.onCancel = this.onCancel.bind(this);
        this.onShow = this.onShow.bind(this);
    }

    render() {
        const {visible, loading, templateType, templateName} = this.state;

        return (
            <WeaDialog
                visible={visible}
                loading={loading}
                title={getLabel(131290, '非标功能管理')}
                icon="icon-coms-currency"
                iconBgcolor='#217346'
                style={{width: 790, height: 510}}
                zIndex={100}
                hasScroll={true}
                buttons={this.getButtons()}
                onCancel={this.onCancel}
            >
                <Confirm ref="Confirm"/>
                <WeaRightMenu datas={this.getRightMenus()}>
                    <WeaTab
                        datas={[{key: '0', title: getLabel(387099, '已启用')}, {key: '1', title: getLabel(32386, '未启用')}]}
                        keyParam='key'
                        selectedKey={templateType}
                        onChange={(key) => this.getData(key)}
                        searchType={['base']}
                        searchsBaseValue={templateName}
                        onSearch={(value) => this.getData(templateType, value)}
                    />
                    <WeaTable comsWeaTableStore={tableStore} rowSelection={this.getRowSelection()} scroll={{y: 380}}/>
                </WeaRightMenu>
            </WeaDialog>
        );
    }

    getButtons() {
        const {templateType, selectedRowKeys:keys} = this.state;

        let buttons = [];
        templateType == '0' && buttons.push(<Button type="primary" disabled={!(keys && keys.length)} onClick={this.onUse}>{getLabel(26471, '停用')}</Button>);
        templateType == '1' && buttons.push(<Button type="primary" disabled={!(keys && keys.length)} onClick={this.onUse}>{getLabel(18095, '启用')}</Button>);
        buttons.push(<WeaMoreButton datas={this.getRightMenus()}/>);
        return buttons;
    }

    getRightMenus() {
        const {templateType, selectedRowKeys:keys} = this.state;

        let rightMenus = [];
        templateType == '0' && rightMenus.push({key: '1', disabled: !(keys && keys.length), icon: <i className="icon-coms-Sealed"/>, content: getLabel(26471, '停用'), onClick: this.onUse});
        templateType == '1' && rightMenus.push({key: '2', disabled: !(keys && keys.length), icon: <i className="icon-coms-deblock"/>, content: getLabel(18095, '启用'), onClick: this.onUse});
        return rightMenus;
    }

    getRowSelection() {
        const _self = this;

        return {
            onChange(selectedRowKeys) {
                _self.setState({selectedRowKeys})
            }
        };
    }

    getData(templateType = '0', templateName = '') {
        this.setState({visible: true, loading: true, templateType, templateName});
        WeaTools.callApi('/api/portal/unStandardFuncInfo/list', 'POST', {templateType, templateName}).then(data => {
            tableStore.getDatas(data.sessionkey, 1);
            tableStore.setSelectedRowKeys([]);
            this.setState({loading: false, selectedRowKeys: []});
        });
    }

    onUse() {
        const {templateType, selectedRowKeys:ids} = this.state;
        const method = {'0': 'stop', '1': 'start'}[templateType];

        this.setState({loading: true});
        WeaTools.callApi('/api/portal/unStandardFuncInfo/upgrade', 'POST', {method, ids}).then(data => {
            window.localStorage.removeItem('theme-frontEndMenu');
            window.localStorage.removeItem('theme-backEndMenu');
            this.refs['Confirm'].onShow(templateType, data);
            this.getData(templateType);
            this.setState({loading: false});
        });
    }

    onCancel() {
        this.setState({visible: false});
    }

    onShow() {
        this.getData();
    }
}

export default WeaNonStandard;
